// 引入axios封装
import axios from 'axios'
import { message } from 'ant-design-vue'
import { tansParams } from '@/utils/index'

// 创建axios实例
const request = axios.create({
  baseURL: '/', // url = base url + request url
  timeout: 5000 // 5s超时
})

// 请求拦截器 一般写法模式
request.interceptors.request.use(
  (response) => {
    // 是否需要防止数据重复提交
    if (response.method === 'get' && response.params) {
      let url = response.url + '?' + tansParams(response.params)
      url = url.slice(0, -1)
      response.params = {}
      response.url = url
    }
    return response // 请求成功则返回response
  },
  (error) => {
    // 请求失败则显示错误状态
    message.error(error.message)
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    message.error(error.message)
    return Promise.reject(error)
  }
)

// 导出request
export default request
